package cn.sdhuijin.healthexam.control;

import java.util.Properties;

import android.app.Activity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import cn.sdhuijin.healthexam.util.Constants;

public class CheckBoxGroupViewControl implements IControl,
		OnCheckedChangeListener {

	private Activity activity;
	private int[] checkBoxIds;
	private String name;
	private int etId;
	private String etName;
	private String enableStr;

	public CheckBoxGroupViewControl(Activity activity, int[] checkBoxIds,
			String name, int etId, String etName, String enableStr) {
		this.activity = activity;
		this.checkBoxIds = checkBoxIds;
		this.name = name;
		this.etId = etId;
		this.etName = etName;
		for (int id : checkBoxIds) {
			CheckBox bt = (CheckBox) activity.findViewById(id);
			bt.setOnCheckedChangeListener(this);
		}
		this.enableStr = enableStr;
	}

	@Override
	public void getValue(Properties prop) {
		try {
			StringBuffer buffer = new StringBuffer();
			StringBuffer buffer_id = new StringBuffer();
			for (int i = 0; i < checkBoxIds.length; i++) {
				CheckBox checkbox = (CheckBox) activity
						.findViewById(checkBoxIds[i]);
				if (checkbox.isChecked()) {
					buffer.append(checkbox.getText().toString());
					buffer.append(Constants.SPLIT_TAG);
					buffer_id.append(i + 1);
					buffer_id.append(Constants.SPLIT_TAG);
				}
			}
			if (buffer != null && !Constants.STR_NULL.equals(buffer.toString())) {
				prop.setProperty(this.name + "_content", buffer.toString());
				prop.setProperty(this.name, buffer_id.toString());
			} else {
				prop.setProperty(this.name + "_content", "");
				prop.setProperty(this.name, "");
			}
			EditText editText = (EditText) this.activity.findViewById(etId);
			String edtValue = editText.getText().toString();
			if (edtValue != null) {
				prop.setProperty(etName, edtValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setValue(Properties prop) {
		try {
			String value = prop.getProperty(this.name + "_content");
			if (value != null && !value.equals("")) {
				String[] str = value.split(Constants.SPLIT_TAG);
				for (int i = 0; i < checkBoxIds.length; i++) {
					for (int j = 0; j < str.length; j++) {
						CheckBox checkBox = (CheckBox) activity
								.findViewById(checkBoxIds[i]);
						if (str[j].equals(checkBox.getText().toString())) {
							checkBox.setChecked(true);
						}
					}
				}
				EditText editText = (EditText) this.activity.findViewById(etId);
				editText.setEnabled(true);
				String edtValue = prop.getProperty(etName);
				if (edtValue != null) {
					editText.setText(edtValue);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton cb, boolean flag) {
		Log.i("点击", cb.getText().toString() + "  " + flag);
		if (cb.getText().toString().equals(enableStr)) {
			if (flag == true) {
				for (int i = 0; i < checkBoxIds.length; i++) {
					CheckBox checkBox = (CheckBox) activity
							.findViewById(checkBoxIds[i]);
					if (!(enableStr).equals(checkBox.getText().toString())) {
						checkBox.setChecked(false);
						checkBox.setClickable(false);
					}
				}
				EditText editText = (EditText) this.activity.findViewById(etId);
				editText.setText("");
				editText.setEnabled(false);
			} else {
				for (int i = 0; i < checkBoxIds.length; i++) {
					CheckBox checkBox = (CheckBox) activity
							.findViewById(checkBoxIds[i]);
					if (!(enableStr).equals(checkBox.getText().toString())) {
						checkBox.setClickable(true);
					}
				}
				EditText editText = (EditText) this.activity.findViewById(etId);
				editText.setEnabled(true);
			}
		}

	}

}
